<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $this->load->model('Schedule_model');
    }
	

	public function index($agid = '')
	{
        if (empty($agid)) {
            $userid = $this->thatUser['id'];
        } else {
            $userid = _get_key_val($agid, true, false);
            $this->load->model('User_model');
            $user = $this->User_model->get_by_id($userid);
        }
        if (!$userid)
        {
        	alert('无此用户！',base_url().'/m/fans');
        }
        $time_sort = $this->input->get('time_sort');
        $type_sort = $this->input->get('type_sort');
        $sort = 'dodate desc';
        $arrParam = array();
        if ($time_sort)
        {
        	if ($time_sort == 1)
        	{
        		$sort = 'dodate asc';
        	}
        	$arrParam['time_sort'] = $time_sort;
        }
        if ($type_sort)
        {
        	if ($type_sort == 1)
        	{
        		$sort = 'type asc';
        	}
        	else
        	{
        		$sort = 'type desc';
        	}
        	$arrParam['type_sort'] = $type_sort;
        }
		$page     = _get_page();
		$pagesize = 10;
		$arrWhere = array('userid'=>$userid,'status'=>1);		//条件

		$list = $this->Schedule_model->fetch_page($page, $pagesize, $arrWhere, '*', $sort);
		//echo $this->db->last_query();die;
		

		//分页
		$pagecfg = array();
		$pagecfg['base_url']     = _create_url('m/schedule', $arrParam);
		$pagecfg['total_rows']   = $list['count'];
		$pagecfg['cur_page'] = $page;
		$pagecfg['per_page'] = $pagesize;
		//$this->load->library('pagination');
		$this->pagination->initialize($pagecfg);
		$list['pages'] = $this->pagination->create_links();

		$result = array(
			'list' => $list,
            'agid' => $userid,
			'arrParam' => $arrParam
			);
		if (isset($user))
		{
			$result['user_info'] = $user;
		}
		
		//print_r($list);
		$this->load->view('m/schedule',$result);
	}

	public function addschedule()
	{
            $res = array('code'=>0,'data'=>array());
		
            if ($this->input->is_post())
            {
                //验证规则
                $config = array(
                    array(
                        'field'   => 'dodate', 
                        'label'   => '时间', 
                        'rules'   => 'trim|required'
                    ),
                    array(
                        'field' => 'enddate',
                        'label' => '结束时间',
                        'rules' => 'trim|required'
                    ),

                    array(
                         'field'   => 'thing', 
                         'label'   => '行程', 
                         'rules'   => 'trim|required'
                    ),
                );

                $this->form_validation->set_rules($config);

                if ($this->form_validation->run() === TRUE)
                {
                    $res['data'] = '添加成功';
                    $id = _get_key_val($this->input->post('id'), TRUE);
                    $userid = $this->input->post('userid');
                    // 实现机构和个人行程的统一
                    if (!empty($userid)) {
                        $userid = _get_key_val($userid, true);
                    } else {
                        $userid = $this->thatUser['id'];
                    }

                    // 开始时间和结束时间
                    $doDate = strtotime($this->input->post('dodate')) ;
                    $endDate = strtotime($this->input->post('enddate'));
                    
                    // 编辑
                    if($id) {
                        $data = array(
                            'id' => $id,
                            'dodate' => $doDate ? $doDate : time(),
                            'enddate' => $endDate ? $endDate : time(),
                            'thing' => $this->input->post('thing'),
                        	'type' => $this->input->post('type'),
                        	'status' => 1,
                            'op_userid' => $this->loginID,
                            'op_username' => $this->loginUserName,
                            'op_time' => time(),
                            'time_zone' => $this->input->post('time_zone')
                        );
                        $res['data'] = '修改成功';
                    } else {
                        $data = array(
                            'userid'=> $userid,
                            'dodate' => $doDate ? $doDate : time(), // 默认时间为当前时间
                            'enddate' => $endDate ? $endDate : time(),
                            'thing' => $this->input->post('thing'),
                            'addtime' => time(),
                        	'type' => $this->input->post('type'),
                            'status' => 1, // 
                            'op_userid'=>$this->loginID,
                            'op_username'=>$this->loginUserName,
                            'op_time' => time(),
                            'time_zone' => $this->input->post('time_zone')
                        );
                    }

                    $this->Schedule_model->insert($data);
                    $res['code'] = 200;

                    } else {
                        $res['data']['error_messages'] = $this->form_validation->getErrors();
                    }

		}//-is_post()
		$this->view->json($res);
		exit;

	}

	public function getinfo(){
		$res = array('code'=>0,'data'=>array());
		$id	= _get_key_val($this->input->get('id'), TRUE);
		$o = $this->Schedule_model->get_info_by_id($id,'dodate,thing,enddate,type,time_zone');
// 		echo $this->Schedule_model->db->last_query();die;
		if($o)
		{
			$o['dodate'] = date('Y-m-d',$o['dodate']);
                        $o['enddate'] = date('Y-m-d', $o['enddate']);
			$res['code'] = 200;
			$res['data'] = $o;
		}

		$this->view->json($res);
		exit;

	}

	public function del(){
		$res = array('code'=>0,'data'=>array());
		$id	= _get_key_val($this->input->post('id'), TRUE);
		$userid = $this->input->post('userid', true);
        
        if (!empty($userid)) {
            $userid = _get_key_val($userid, true, false);
        } else {
            $userid = $this->thatUser['id'];
        }
        
		$this->Schedule_model->update_by_where(array('id'=>$id, 'userid'=>$userid),array('status'=>0));
		$res['code'] = 200;

		$this->view->json($res);
	}
}